

import bpy
from bpy.types import Operator
from 全局控制.Node.节点和插座基类.bl_UpdateNode import *
import 全局控制.Node.节点和插座基类.bl_节点基类 as 基类

import pdb



E_域采样类型 = [
	("TYPE_采样3X3", "采样3X3", "", 0),
	("TYPE_采样X6", "X6", "", 1),
]

def f_update_域采样方式(self, context):
	if self.m_采样类型 == 'TYPE_采样3X3' :
		if self.outputs['索引'].f_get维度() != 27 :
			self.outputs['索引'].f_set维度(27)
	elif self.m_采样类型 == 'TYPE_采样X6' :
		if self.outputs['索引'].f_get维度() != 7 :
			self.outputs['索引'].f_set维度(7)

class C_SceneNode_3X3X3域单元查找(基类.C_SN_基类):
	bl_dname = 'bl_node_域单元查找'
	bl_label = '3X3 域单元查找'
	
	m_方向 = bpy.props.IntVectorProperty(default=(0,0,0), min=0)
	m_采样类型 = bpy.props.EnumProperty(items = E_域采样类型, update=f_update_域采样方式, default="TYPE_采样X6")

	def init(self, context):
		super(C_SceneNode_3X3X3域单元查找, self).f_init(context)
		self.inputs.new('C_NodeSocket_3i', name='维度')
		self.inputs.new('C_NodeSocket_i', name='查找坐标')
		self.inputs.new('C_NS_List_T', name='MASK')
		#self.inputs['维度'].f_setData([1,1,1])
		
		self.outputs.new('C_NodeSocket_ni', name='索引')
		self.outputs['索引'].f_set维度(7)
		
		print('f_set A = ', id(self.outputs['索引']))

	def draw_buttons(self, context, layout):
		layout.prop(self, 'm_采样类型')
		
	def f_update(self, 是否向下传递=False, input插座=None):
		维度 = self.inputs['维度'].f_getData()
		
		查找索引 = self.inputs['查找坐标'].f_getData()
		print('维度 = ', 维度, 查找索引)
		if type(查找索引).__name__ == 'list':
			查找索引 = 查找索引[0]
		#print('查找索引 = ', 查找索引)
		切片大小 = 维度[0]*维度[1]
		z = int(查找索引 / 切片大小)
		所在切片数量 = (查找索引 - int(z * 切片大小))
		y = int( 所在切片数量 / 维度[0] )
		x = 所在切片数量 - y*维度[0]
		#print('维度 = ', 维度)
		#print('切片大小 = ', 切片大小)
		#print('所在切片数量 = ', 所在切片数量)
		#print('所在切片数量 = ', x, y, z)

		#底层								#中层								#顶层
		###############################		###############################		###############################
		#  x - 1  #  x      #  X + 1  #		#  x - 1  #  x      #  X + 1  #		#  x - 1  #  x      #  X + 1  #
		#  y + 1  #  y + 1  #  Y + 1  #		#  y + 1  #  y + 1  #  Y + 1  #		#  y + 1  #  y + 1  #  Y + 1  #
		#  z - 1  #  z - 1  #  z - 1  #		#  z      #  z      #  z      #		#  z + 1  #  z + 1  #  z + 1  #
		###############################		###############################		###############################
		#  x - 1  #  x      #  X + 1  #		#  x - 1  #  x      #  X + 1  #		#  x - 1  #  x      #  X + 1  #
		#  y      #  y      #  y      #		#  y      #  y      #  y      #		#  y      #  y      #  y      #
		#  z - 1  #  z - 1  #  z - 1  #		#  z      #  z      #  z      #		#  z + 1  #  z + 1  #  z + 1  #
		###############################		###############################		###############################
		#  x - 1  #  x      #  X + 1  #		#  x - 1  #  x      #  X + 1  #		#  x - 1  #  x      #  X + 1  #
		#  y - 1  #  y - 1  #  y - 1  #		#  y - 1  #  y - 1  #  y - 1  #		#  y - 1  #  y - 1  #  y - 1  #
		#  z - 1  #  z - 1  #  z - 1  #		#  z      #  z      #  z      #		#  z + 1  #  z + 1  #  z + 1  #
		###############################		###############################		###############################
		
		index = 0
		if self.m_采样类型 == 'TYPE_采样3X3' :
			
			ID = [-1] * 27
			for zOffset in range(-1, 2):
				z坐标 = z+zOffset
				

				for yOffset in range(-1, 2):
					y坐标 = y+yOffset
					
					for xOffset in range(-1, 2):
						x坐标 = x+xOffset
						index += 1

						if z坐标 < 0 or z坐标 >= 维度[2]: continue
						if y坐标 < 0 or y坐标 >= 维度[1]: continue
						if x坐标 < 0 or x坐标 >= 维度[0]: continue
							
						#print('index = ', index)
						ID[index-1] = (切片大小*z坐标) + (维度[0]*y坐标) + x坐标
			ID.remove(13)
			ID.index(0, 查找索引)

		elif self.m_采样类型 == 'TYPE_采样X6' :
			ID = [-1] * 7
			ID[0] = 查找索引
			for Offset in [-1, 1]:
				x坐标 = x+Offset
				index += 1
				if x坐标 >= 0 and x坐标 < 维度[0]:
					ID[index] = (切片大小*z) + (维度[0]*y) + x坐标

				y坐标 = y+Offset
				index += 1
				if y坐标 >= 0 and y坐标 < 维度[1]:
					ID[index] = (切片大小*z) + (维度[0]*y坐标) + x

				z坐标 = z+Offset
				index += 1
				if z坐标 >= 0 and z坐标 < 维度[2]:
					ID[index] = (切片大小*z坐标) + (维度[0]*y) + x
		#self.outputs

		if self.inputs['MASK'].is_linked :
			data = self.inputs['MASK'].f_getData()
			for i in range(len(ID)):
				index = ID[i]
				if index >= 0 and data[index] == False :
					ID[i] = -1

		self.outputs['索引'].f_setData(ID)
		super(C_SceneNode_3X3X3域单元查找, self).f_update(是否向下传递, input插座)



class C_NODE_MENU_域单元查找(bpy.types.Operator):
	bl_idname = 'node.global_node_find_3x3x3_domain_guid'
	bl_label = 'Add 域单元查找'
	bl_options = {'REGISTER', 'UNDO'}

	def execute(operator, context):
		operator.use_transform = True
		bpy.ops.node.add_node(type='C_SceneNode_3X3X3域单元查找', use_transform=True)
		return {'FINISHED'}









class C_SN_域采样器_索引阻塞单元方向(基类.C_SN_基类):
	bl_dname = 'bl_node_域采样器_索引阻塞单元方向'
	bl_label = '域采样器_索引阻塞单元方向'
	
	m_方向 = bpy.props.IntVectorProperty(default=(0,0,0), min=0)
	m_采样类型 = bpy.props.EnumProperty(items = E_域采样类型, update=f_update_域采样方式, default="TYPE_采样X6")
	def init(self, context):
		super(C_SN_域采样器_索引阻塞单元方向, self).f_init(context)

		self.inputs.new('C_NS_List_T', name='采样体素索引组')
		self.inputs.new('C_NS_List_T', name='查找轴索引')
		
		self.outputs.new('C_NodeSocket_i', name='方向')
		self.outputs.new('C_NodeSocket_i', name='索引下标')

		self.outputs.new('C_NS_List_T', name='邻接物体索引')
		#self.outputs.new('C_NodeSocket_i', name='物体索引')
		self.outputs['邻接物体索引'].f_set维度(0)
		
	'''def update(self):
		矩阵插座 = self.inputs['输入坐标']
		rut = 插件基类.f_连接到插座是否为正确类型(矩阵插座, 'C_NodeSocket_N矩阵_f')
		if rut == -1 :
			self.color = (1.0 , 0.0, 0.0)
		else:
			self.color = (0.5 , 0.5, 0.5)'''
	
	def f_update(self, 是否向下传递=False, input插座=None):
		#pdb.set_trace()
		Indexs = self.inputs['采样体素索引组'].f_getData()
		findIndex = self.inputs['查找轴索引'].f_getData()
		
		邻接物体索引 = []
		#print('findIndex = ', Indexs, findIndex)
		length = len(Indexs)
		轴 = -1
		轴下标 = -1
		物体索引 = -1
		if length == 7 :
			for i in findIndex:
				轴下标 = i
				if Indexs[i+1] == -1 and Indexs[i+4] != -1:
					轴 = i
					break
				elif Indexs[i+1] != -1 and Indexs[i+4] == -1:
					轴 = -i
					break
			
			for i in Indexs:
				if i >= 0 :
					邻接物体索引.append(i)
			#print('邻接物体索引 = ', 邻接物体索引)
			if len(邻接物体索引) : 邻接物体索引.remove(邻接物体索引[0])

		elif length == 27 :
			pass

		#print('邻接物体索引 = ', 邻接物体索引)
		self.outputs['方向'].f_setData(轴)
		self.outputs['索引下标'].f_setData(轴下标)
		self.outputs['邻接物体索引'].f_setData(邻接物体索引)

		super(C_SN_域采样器_索引阻塞单元方向, self).f_update(是否向下传递, input插座)
		
class C_NODE_MENU_域采样器_索引阻塞单元方向(bpy.types.Operator):
	bl_idname = 'node.global_node_find_domain_guid_blocking_dir'
	bl_label = '域采样器_索引阻塞单元方向'
	bl_options = {'REGISTER', 'UNDO'}

	def execute(operator, context):
		operator.use_transform = True
		bpy.ops.node.add_node(type='C_SN_域采样器_索引阻塞单元方向', use_transform=True)
		return {'FINISHED'}



classes = (
	C_SceneNode_3X3X3域单元查找,
	C_NODE_MENU_域单元查找,

	C_SN_域采样器_索引阻塞单元方向,
	C_NODE_MENU_域采样器_索引阻塞单元方向,
)

def register():
	for c in classes:
		bpy.utils.register_class(c)
		

def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)



